package com.kaili.basic.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kaili.common.util.CompareUtil;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.*;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import java.lang.reflect.Method;

@EnableCaching
@Configuration
public class CacheConfig extends CachingConfigurerSupport {
    private int expireTime = 60;

    /**
     * 生成key的策略
     *
     * @return
     */
    @Bean
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                String[] value = new String[1];
               /* String companyId;
                if(params[0].toString().length()!=17){
                    companyId = CommonUtil.getCompanyId();
                    if(null != companyId){
                        sb.append(companyId).append("-");
                    }
                }*/
                Cacheable cacheable = method.getAnnotation(Cacheable.class);
                if(null != cacheable){
                    value =cacheable.value();
                }
                CachePut cachePut = method.getAnnotation(CachePut.class);
                if(null != cachePut){
                    value =cachePut.value();
                }
                CacheEvict cacheEvict = method.getAnnotation(CacheEvict.class);
                if(null != cacheEvict){
                    value = cacheEvict.value();
                }
                String[] values = value[0].split("#");
                sb.append(values[0]).append("-");
                if(values.length>1){
                    String valueStr = null;
                    for (Object obj : params) {
                        valueStr = CompareUtil.getKey(obj, values[1]);
                        if(null != valueStr){
                            sb.append(valueStr);
                            break;
                        }
                    }
                }else{
                    if(params!=null && params.length>0){
                        sb.append(params[0].toString());
                    }
                }
//                sb.append(cacheable.value());//类名
//                sb.append("-");
//                sb.append(method.getName());//方法名
//                sb.append("-");
//                for (Object obj : params) {
//                    sb.append(obj.toString());//参数
//                }
//                System.out.println(sb.toString());
                return sb.toString();
            }
        };
    }

    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        //Number of seconds before expiration. Defaults to unlimited (0)
        cacheManager.setDefaultExpiration(expireTime); //设置key-value超时时间
        return cacheManager;
        //return new RedisCacheManager(redisTemplate);
    }

    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}
